package com.moss.common.util;

import com.moss.common.constant.Constants;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author gxcc3
 * @date: 2024/8/22 22:11
 */
public class IdUtil {



    private static final String DATE_TIME_STR_PATTERN = "yyyyMMddHHmmssSSS";

    private static final DateTimeFormatter dateTimeStrFormatter = DateTimeFormatter.ofPattern(DATE_TIME_STR_PATTERN);

    private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").withZone(ZoneId.systemDefault());
    private static final AtomicInteger COUNTER = new AtomicInteger(0);


    /**
     * 获取uuid
     * @return
     */
    public static String randomUUID(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(randomUUID());
        }
    }


    /**
     * 获取时间序列化id yyyyMMddHHmmssSSS+5位原子递增数
     * @return
     */
    public static String randomDateId() {
        String timestamp = DTF.format(Instant.now());
        int intNextVal = COUNTER.incrementAndGet();
        if (intNextVal < 1000){
            return timestamp + String.format("%03d", intNextVal);
        }else {
            return timestamp + intNextVal;
        }
    }

    public static synchronized String randomTaskNo() {
        return dateTimeStrFormatter.format(LocalDateTime.now());
    }

    public static String randomSnowId() {
        return String.valueOf(SnowflakeIdWorker.generateId());
    }

}
